const sass = require("sass");
const loadGruntTasks = require("load-grunt-tasks");
const im = require('imagemagick');
const fs = require('fs');
module.exports = (grunt) => {
  grunt.initConfig({
    sass: {
      options: {
        sourceMap: false,
        implementation: sass,
      },
      main: {
        files: {
          "dist/src/assets/styles/main.css": "src/assets/styles/main.scss",
        },
      },
    },
    babel: {
      options: {
        sourceMap: false,
        presets: ["@babel/preset-env"],
      },
      main: {
        files: {
          "dist/src/assets/scripts/main.js": "src/assets/scripts/main.js",
        },
      },
    },
    watch: {
      js: {
        files: ["src/assets/scripts/*.js"],
        tasks: ["babel"],
      },
      css: {
        files: ["src/assets/styles/*.scss"],
        tasks: ["sass"],
      },
    },
    img: {
      task1: {
        src: ["src/assets/images/**/*.png"],
        dest: "dist/src/assets/images",
      }
    },
    svgmin: {
      options: {
        plugins: [
          {
            removeViewBox: false,
          },
          {
            removeUselessStrokeAndFill: false,
          },
          {
            removeAttrs: {
              attrs: ["xmlns"],
            },
          },
        ],
      },
      dist: {
        files: {
          "dist/src/assets/images/brands.svg": "src/assets/images/brands.svg",
        },
      },
    },
    iconfont: {
      options: {},
      your_target: {
        fontName: "my-font-name",
        src: "src/assets/images/*.svg",
        dest: "dist/src/assets/fonts/",
      },
    },

    htmlmin: {
      dist: {
        options: {
          removeComments: true,
          collapseWhitespace: true,
        },
        files: {
          "dist/src/index.html": "src/index.html",
          "dist/src/about.html": "src/about.html",
          "dist/src/layouts/basic.html": "src/layouts/basic.html",
          "dist/src/partials/footer.html": "src/partials/footer.html",
          "dist/src/partials/header.html": "src/partials/header.html",
        },
      },
    },
    clean: {
      temp: "dist/**",
    },
  });
  grunt.registerTask('copy', () => {
    if (!fs.existsSync('./dist/public')) {
      fs.mkdirSync('./dist/public');
    }
    fs.copyFileSync('./public/favicon.ico',"./dist/public/favicon.ico",err=>{
      console.log(err)
    })
  })
  // grunt.loadNpmTasks('grunt-sass')
  loadGruntTasks(grunt); // 自动加载所有的 grunt 插件中的任务

  grunt.registerTask("default", [
    "clean",
    "sass",
    "babel",
    "img",
    "svgmin",
    "iconfont",
    "htmlmin",
    "copy",
    "watch",
  ]);
};
